//
//  JJTabBarViewController.m
//  DemoPro
//
//  Created by vpjacob on 2017/4/7.
//  Copyright © 2017年 vpjacob. All rights reserved.
//

#import "JJTabBarViewController.h"

#ifndef kScreenWidth
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#endif
#ifndef kScreenHeight
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#endif

const NSInteger KNTabBarButtonTag = 999;
const NSInteger KNTabBarButtonTitleLabelTag = 666;

@interface JJTabBarViewController ()

@property (nonatomic, strong) UIView *tabBarView;
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) NSArray *selectImgArray;
@property (nonatomic, strong) NSArray *unSelectImgArray;
@property (nonatomic, strong) NSArray *titlesArray;
@property (nonatomic, strong) NSMutableArray *buttonsArray;
@property (nonatomic, strong) UIColor *titleSelectColor;
@property (nonatomic, strong) UIColor *titleDefaultColor;
@property (nonatomic, copy) NSString *titleFontName;
//@property (nonatomic, strong) KNTabBarModel *tabBarModel;

@end

@implementation JJTabBarViewController

- (void)dealloc {
    [self.view removeObserver:self forKeyPath:@"frame"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    self.view.autoresizesSubviews = NO;
    
    self.buttonsArray = [NSMutableArray array];
    
    self.titleDefaultColor = [UIColor knBlackColor];
    self.titleSelectColor = [UIColor knThemeColor];
    
    //获取系统当前字体
    UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
    self.titleFontName = font.fontName;
    
    self.titlesArray = @[@"首页",@"发现",@"购物车",@"我的"];
    self.selectImgArray = @[@"tab_homeH",@"tab_findH",@"tab_shopH",@"tab_meH"];
    self.unSelectImgArray = @[@"tab_home",@"tab_find",@"tab_shop",@"tab_me"];
    
    
    [self creatViewControllers];
    [self.view addSubview:self.tabBarView];
    
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationItem setHidesBackButton:NO];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if([keyPath isEqualToString:@"frame"]) {
        self.tabBarView.frame = CGRectMake(0, self.view.frame.size.height - 49, kScreenWidth, 49);
    }
}

- (void)creatViewControllers {
    self.meVC = [[JJMeViewController alloc] init];
    self.shopVC = [[JJShopViewController alloc] init];
    self.homeVC = [[JJHomeViewController alloc] init];
    self.findVC = [[JJFindViewController alloc] init];
    
    self.viewControllers = @[self.homeVC,self.findVC,self.shopVC,self.meVC];
    
    NSInteger tabCount = self.titlesArray.count;
    for (int i = 0; i < tabCount; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i + KNTabBarButtonTag;
        [button addTarget:self action:@selector(tabBarPressed:) forControlEvents:UIControlEventTouchUpInside];
        CGFloat buttonWidth = kScreenWidth/tabCount;
        button.frame = CGRectMake(buttonWidth * i, 0, buttonWidth, 49);
        
        // 底部label
        CGRect titleLabelFrame = CGRectMake(0, 29, buttonWidth, 20);
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleLabelFrame];
        titleLabel.tag = KNTabBarButtonTitleLabelTag;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = self.titleDefaultColor;
        titleLabel.font = [UIFont systemFontOfSize:10.0];
        [button addSubview:titleLabel];
        
            titleLabel.text = self.titlesArray[i];
            button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 0);
            [button setImage:[UIImage imageNamed:self.unSelectImgArray[i]] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:self.selectImgArray[i]] forState:UIControlStateSelected];
        
        if (i == 0) {
            titleLabel.textColor = self.titleSelectColor;
            [self tabBarPressed:button];
            button.selected = YES;
        }
        [self.tabBarView insertSubview:button atIndex:2];
        [self.buttonsArray addObject:button];
    }
}

- (UIView *)tabBarView {
    if (!_tabBarView) {
        CGRect tabBarFrame = CGRectMake(0, kScreenHeight - 49, kScreenWidth, 49);
        _tabBarView = [[UIView alloc] initWithFrame:tabBarFrame];
        _tabBarView.backgroundColor = [UIColor whiteColor];
        _tabBarView.layer.shadowColor = [UIColor knLightGrayColor].CGColor;
        _tabBarView.layer.shadowRadius = 1;
        _tabBarView.layer.shadowOffset = CGSizeMake(0, -1); //偏移量
        _tabBarView.layer.shadowOpacity = 0.5;//阴影透明度
        [_tabBarView addSubview:self.bgImageView];
    }
    return _tabBarView;
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.frame = CGRectMake(0, 0, kScreenWidth, 49);
    }
    return _bgImageView;
}

- (void)tabBarPressed:(UIButton *)button {
    if (button.isSelected){
        return;
    }
    NSUInteger index = button.tag - KNTabBarButtonTag;
    self.selectedIndex = index;
    if (self.selectBlock) {
        self.selectBlock(index);
    }
    UIViewController *currentViewController = self.viewControllers[index];
    self.title = currentViewController.title;
    self.navigationItem.rightBarButtonItem = currentViewController.navigationItem.rightBarButtonItem;
    self.navigationItem.leftBarButtonItem = currentViewController.navigationItem.leftBarButtonItem;
    for (UIButton *tabButton in _buttonsArray) {
        BOOL buttonSelect = [tabButton isEqual:button];
        tabButton.selected = buttonSelect;
        
        UILabel *titleLabel = [tabButton viewWithTag:KNTabBarButtonTitleLabelTag];
        titleLabel.textColor = buttonSelect ? self.titleSelectColor : self.titleDefaultColor;
    }
}


- (void)turnToControllerIndex:(int)index {
    if (index < _buttonsArray.count) {
        [self tabBarPressed:_buttonsArray[index]];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
